﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;

namespace Einstein.Group.DAL
{
    public class DeptInfoDAL
    {
        public IList<DeptInfo> SelectAll()
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.DeptInfo.ToList<DeptInfo>();
            }
        }

        public DeptInfo SelectByID(int id)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.DeptInfo.FirstOrDefault<DeptInfo>(c=>c.DeptID == id);
            }
        }

        public bool Add(DeptInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                entities.DeptInfo.AddObject(info);
                return entities.SaveChanges() > 0;
            }
        }

        public bool Exist(DeptInfo info)
        {
            using (EinsteinEntities entities = new EinsteinEntities())
            {
                return entities.DeptInfo.Count(a => a.DeptName == info.DeptName && a.ParentID == info.ParentID && a.DeptID != info.DeptID) > 0;
            }
        }

        public bool Update(DeptInfo info)
        {

            using (EinsteinEntities entities = new EinsteinEntities())
            {
                foreach (var item in entities.DeptInfo.Where(a => a.DeptID == info.DeptID))
                {
                    item.DeptName = info.DeptName;
                    item.Desc = info.Desc;
                }
                return entities.SaveChanges() > 0;
            }
        }

        public bool Delete(DeptInfo info)
        {
            using (EinsteinEntities context = new EinsteinEntities())
            {
                foreach (var item in context.DeptInfo.Where(a => a.DeptID == info.DeptID))
                {
                    context.DeleteObject(item);
                }
                return context.SaveChanges() > 0;
            }
        }


    }
}
